package com.yc.vote.mapper;

import com.yc.vote.entity.Vote;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lyl
 * @since 2022-11-06
 */
public interface VoteMapper extends BaseMapper<Vote> {

    @Insert("insert into vote values(default,#{vname},#{vtype},#{startDate},#{endDate})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(Vote vote);


//    @Select("select a.*," +
//            "(select count( DISTINCT usid) from record b where a.id=b.vid group by vid) users" +
//            "from vote a order by id desc limit #{begin}, #{size}")
    @Select("select a.*," +
            "(select count( DISTINCT usid) from record b where a.id=b.vid group by vid) users " +
            "from vote a order by id desc limit #{begin}, #{size}")
    @Results(
            id = "rm1",
            value = {
                   @Result(column = "id", property = "id", id = true),
                   @Result(column = "start_date", property = "startDate"),
                   @Result(column = "end_date", property = "endDate"),
                   @Result(column = "id", property = "items",
                        many = @Many(
                                // 接口类路径就是命名空间
                                select = "com.yc.vote.mapper.ItemsMapper.selectByVid"
                                // fetchType= FetchType.LAZY
                        )),
            }
    )
    List<Vote> selectAll(@Param("begin") int begin, @Param("size") int size);


    @Select("select a.*," +
            " (select count( DISTINCT usid) from record b where a.id=b.vid group by vid) users" +
            " from vote a where id = #{id}")
    @ResultMap("rm1")
    Vote selectById(String id);
}
